<template>
	<view>
		<Navbar :navbarData="CGname" />
		<view class="head">
			<view class="flex-cb" style="padding-bottom: 30rpx;">
				<view class="head-left">
					<view v-if="nickName">欢迎回来，{{ nickName }}</view>
					<view v-else @click="login">请登录</view>
					<view>{{ date }}</view>
				</view>
				<view class="head-right" @click="personClick">个人资料</view>
			</view>
			<view v-if="userList['OMPHTodayData']" class="head-tj flex-c">
				<view class="head-tjcnt" v-for="(item, index) in headList" :key="index" @click="jumps(item.value, index)">
					<view>{{ item.name }}</view>
					<view>
						<text v-if="index === 0">￥</text>
						{{ item.value }}
					</view>
				</view>
			</view>
		</view>
		<view class="content">
			<view v-if="iconListb && iconListb.length > 0" class="flex-c" style="flex-wrap: wrap;">
				<view class="icon" v-for="item,index in iconListb" :key="index" @click="jumpa(item.id)">
					<image :src="item.icon"></image>
					<view>{{ item.name }}</view>
				</view>
			</view>
			<view v-if="userList['OMPHWaitDone']" class="daiban flex-cb">
				<view>我的待办({{ totalCount }})</view>
				<view @click="jumpb">更多 ></view>
			</view>
			<block v-if="userList['OMPHWaitDone']">
				<view v-if="list && list.length > 0" style="padding: 30rpx 0;">
					<view class="li on" v-for="item,index in list" :key="index">
						<view class="title">
							<text>{{ item.title }}</text>
							{{ item.content }}
						</view>
						<view class="chuli flex-cb">
							<text>{{ item.sendTime }}</text>
							<text class="btn" @click="chuli(item)">处理</text>
						</view>
					</view>
				</view>
				<view class="no-list" v-if="!list || list.length == 0">
					<image src="@/static/images/default_empty.png"></image>
					<view>暂无数据</view>
				</view>
			</block>
		</view>
	</view>
</template>

<script>
	import Navbar from '@/components/navbar.vue'
	let Qrcode = require('@/utils/reqrcode.js')
	// #ifdef H5
	import wx from '@/common/jweixin.js';
	// #endif
	export default {
		components: {
			Navbar
		},
		data() {
			return {
				CGname: '',
				nickName: '',
				date: '',
				userList: {},
				headList: [{
						name: '今日营收',
						value: '0'
					},
					{
						name: '今日订单',
						value: '0'
					},
					{
						name: '今日入场',
						value: '0'
					}
				],
				iconListb: [],
				iconList: [{
						name: '场地预定',
						id: 0,
						icon: '/static/images/home_nav_1.png'
					},
					{
						name: '资金流水',
						id: 8,
						icon: '/static/images/home_nav_jiesuan.png'
					},
					{
						name: '经营分析',
						id: 2,
						icon: '/static/images/home_nav_jiankong.png'
					},
					{
						name: '门禁管控',
						id: 3,
						icon: '/static/images/home_nav_4.png'
					},
					{
						name: '灯光管控',
						id: 4,
						icon: '/static/images/home_nav_5.png'
					},
					{
						name: '监控设备',
						id: 5,
						icon: '/static/images/home_nav_6.png'
					},
					{
						name: '扫码核验',
						id: 7,
						icon: '/static/images/home_nav_8.png'
					},
					{
						name: '会员核验',
						id: 10,
						icon: '/static/images/home_nav_dengkong.png'
					},
					{
						name: '活动核验',
						id: 11,
						icon: '/static/images/home_nav_huodongheyan.png'
					},
					{
						name: '停车券',
						id: 12,
						icon: '/static/images/home_nav_tingchequan.png'
					},
					{
						name: '会员管理',
						id: 1,
						icon: '/static/images/home_nav_2.png'
					},
					{
						name: '意见反馈',
						id: 6,
						icon: '/static/images/home_nav_7.png'
					}
				],
				page: 0, //页码
				is: true, //控制是否还能切换下一页
				list: [],
				totalCount: 0
			};
		},
		onLoad() {
			this.getDate();
			this.getSignature();
		},
		onHide: function() {
			this.returnedValue = ''
			this.show = false
		},
		onShow() {
			this.CGname = uni.getStorageSync('venueName');
			this.nickName = uni.getStorageSync('nickName');
			if (uni.getStorageSync('userId') && !uni.getStorageSync('venueName')) {
				this.getCG();
			} else if (uni.getStorageSync('userId') && uni.getStorageSync('venueName') && uni.getStorageSync(
					'GYMID')) {
				this.getHome();
				this.getList();
			}
		},
		methods: {
			getSignature() {
				var url = location.href.split('#')[0];
				this.$app.ajax({
					data: {
						param: {
							path: url
						}
					},
					api: this.$api.wxpConfig()
				}).then((res) => {
					if (res.errorCode === '000000') {
						this.setShare(res.record)
					}
				}).catch(() => {})
			},
			// 设置分享数据
			setShare(signature) {
				let that = this
				var url = location.href.split('?')[0]
				uni.setStorageSync('signature', JSON.stringify(signature))
				wx.config({
					debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					// 配置微信 JSSDK
					appId: signature.appId, // 必填，公众号的唯一标识
					timestamp: signature.timestamp, // 必填，生成签名的时间戳
					nonceStr: signature.nonceStr, // 必填，生成签名的随机串
					signature: signature.signature, // 必填，签名
					jsApiList: ['scanQRCode', 'checkJsApi',
					'chooseImage'], // 必填，需使用的JS接口列表
				})
			
				wx.ready(function() {
					console.log('这是分享功能')
					
				})
				wx.error((res) => {
					console.log('分享err', res)
					// config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
					// that.getSignature()
				})
			},
			// 列表
			getList() {
				this.$app.ajax({
					data: {
						pagination: {
							firstQueryTime: this.$app.firstQueryTime(),
							page: 1,
							rows: 5
						},
						param: {
							venueId: uni.getStorageSync('GYMID'),
							type: '1',
						}
					},
					api: this.$api.backlogListMini()
				}).then((res) => {
					if (res.errorCode != '000000') return
					this.list = res.recordList
					this.totalCount = res.totalCount
				})
			},
			jumps(value, index) {
				if (!this.nickName) {
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}, 500);
					return uni.showToast({
						title: '请先登录',
						icon: 'none',
						duration: 2000
					})
				}
				if (value && value > 0) {
					if (index == 2) {
						uni.navigateTo({
							url: '/pages/index/bepresent'
						})
					} else {
						uni.navigateTo({
							url: '/pages/index/invoiceList'
						})
					}
				}
			},
			login() {
				uni.navigateTo({
					url: '/pages/login/login'
				});
			},
			chuli(item) {
				console.log('id', item);
				let that = this;
				this.$app.ajax({
					data: {
						param: {
							venueId: uni.getStorageSync('GYMID'),
							id: item.id
						}
					},
					api: this.$api.backlogFinishMini()
				}).then((res) => {
					if (res.errorCode != '000000') return;
					uni.showToast({
						title: '处理完成',
						icon: 'success',
						duration: 1000,
						success(res) {
							setTimeout(() => {
								that.list = [];
								that.totalCount = 0;
								that.getList();
							}, 100);
						}
					});
				});
			},
			jumpa(index) {
				if (!this.nickName) {
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						});
					}, 500);
					return uni.showToast({
						title: '请先登录',
						icon: 'none',
						duration: 2000
					});
				}
				if (index == 0) {
					uni.navigateTo({
						url: '/pages/siteReservation/siteReservation'
					});
				} else if (index == 1) {
					uni.navigateTo({
						url: '/pages/member/index'
					});
				} else if (index == 2) {
					uni.navigateTo({
						url: '/pages/analysis/analysis'
					});
				} else if (index == 3) {
					uni.navigateTo({
						url: '/pages/accessControl/accessControl'
					});
				} else if (index == 4) {
					uni.navigateTo({
						url: '/pages/lamp/lamp'
					});
				} else if (index == 5) {
					uni.navigateTo({
						url: '/pages/camera/camera'
					});
				} else if (index == 6) {
					uni.navigateTo({
						url: '/pages/feedbackList/feedbackList'
					});
				} else if (index == 7) {
					// #ifdef MP
					uni.scanCode({
						success(res) {
							uni.navigateTo({
								url: '/pages/verification/details?code=' + res.result
							});
						}
					});
					// #endif
					// #ifdef H5
					this.onScan(index)
					// #endif
				} else if (index == 11) {
					// #ifdef MP
					uni.scanCode({
						success(res) {
							uni.navigateTo({
								url: '/pages/verification/hd?code=' + res.result
							});
						}
					});
					// #endif
					// #ifdef H5
					this.onScan(index)
					// #endif
				} else if (index == 8) {
					uni.navigateTo({
						url: '/pages/fund/fund'
					});
				} else if (index == 10) {
					uni.navigateTo({
						url: '/pages/verification/list'
					});
				} else if (index == 12) {
					uni.navigateTo({
						url: '/pages/index/parking'
					});
				}
			},
			isWeiXin() {
				// #ifndef H5
				return false;
				// #endif
				var ua = navigator.userAgent.toLowerCase();
				if (ua.match(/MicroMessenger/i) == "micromessenger") {
					console.log('微信公众号环境');
					return true;
				} else {
					return false;
				}
			},
			// h5扫描二维码并解析
			onScan(type) {
				if(this.isWeiXin()) {
					wx.scanQRCode({
						needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
						scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码，默认二者都有
						success: function(res) {
							var result = res.resultStr; // 当 needResult 为 1 时，扫码返回的结果
							var resultArr = result.split(','); // 扫描结果以逗号分割数组(一维码)
							var codeContent = resultArr[resultArr.length - 1]; // 获取数组最后一个元素，也就是最终的内容 
							
							if (type == 11) {
								uni.navigateTo({
									url: '/pages/verification/hd?code=' + codeContent
								});
							} else if (type == 7) {
								uni.navigateTo({
									url: '/pages/verification/details?code=' + codeContent
								});
							}
						},
						fail: function(response) {
							console.log("调用wx.scanQRCode扫码失败");
						},
					});
					return false
				}
				uni.chooseImage({
					count: 1,
					success: imgRes => {
						Qrcode.qrcode.decode(this.getObjectURL(imgRes.tempFiles[0]))
						Qrcode.qrcode.callback = (codeRes) => {
							if (codeRes.indexOf('error') >= 0) {
								// 二维码识别失败
								console.log('不合法二维码：', codeRes)
							} else {
								// 二维码识别成功
								let r = this.decodeStr(codeRes)
								uni.navigateTo({
									url: '/pages/verification/details?code=' + r
								});
							}
						}
					}
				})
			},
			// 解码，输出：中文
			decodeStr(str) {
				var out, i, len, c;
				var char2, char3;
				out = "";
				len = str.length;
				i = 0;
				while (i < len) {
					c = str.charCodeAt(i++);
					switch (c >> 4) {
						case 0:
						case 1:
						case 2:
						case 3:
						case 4:
						case 5:
						case 6:
						case 7:
							// 0xxxxxxx
							out += str.charAt(i - 1);
							break;
						case 12:
						case 13:
							// 110x xxxx 10xx xxxx
							char2 = str.charCodeAt(i++);
							out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
							break;
						case 14:
							// 1110 xxxx 10xx xxxx 10xx xxxx
							char2 = str.charCodeAt(i++);
							char3 = str.charCodeAt(i++);
							out += String.fromCharCode(((c & 0x0F) << 12) |
								((char2 & 0x3F) << 6) |
								((char3 & 0x3F) << 0));
							break;
					}
				}
				return out;
			},
			// 获取文件地址函数
			getObjectURL(file) {
				var url = null
				if (window.createObjectURL !== undefined) { // basic
					url = window.createObjectURL(file)
				} else if (window.URL !== undefined) { // mozilla(firefox)
					url = window.URL.createObjectURL(file)
				} else if (window.webkitURL !== undefined) { // webkit or chrome
					url = window.webkitURL.createObjectURL(file)
				}
				return url
			},
			jumpb() {
				if (!this.nickName) {
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}, 500);
					return uni.showToast({
						title: '请先登录',
						icon: 'none',
						duration: 2000
					})
				}
				uni.navigateTo({
					url: '/pages/home/todolist'
				})
			},
			getCG() {
				var that = this
				this.$app.ajax({
					data: {
						param: {
							lat: '',
							lng: '',
							isSearch: '1',
							openStatus: '1'
						}
					},
					api: this.$api.getVenue()
				}).then((res) => {
					this.CGname = res.recordList[0].name
					uni.setStorageSync('venueName', res.recordList[0].name)
					uni.setStorageSync('GYMID', res.recordList[0].id)
					that.getHome()
					that.getList()
				})
			},
			personClick() {
				if (!this.nickName) {
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						});
					}, 500);
					return uni.showToast({
						title: '请先登录',
						icon: 'none',
						duration: 2000
					});
				}
				uni.navigateTo({
					url: '/pages/home/personal'
				});
			},
			getHome() {
				this.$app.ajax({
					data: {
						param: {}
					},
					api: this.$api.resUser()
				}).then((res) => {
					if (res.errorCode != '000000') return;
					let obj = res.record;
					let iconList = this.iconList;
					let list = [];
					iconList.forEach(s => {
						if (s.name == '场地预定' && obj['OMPHSite']) {
							list.push(s);
						} else if (s.name == '资金流水' && obj['OMPHFlowFunds']) {
							list.push(s);
						} else if (s.name == '经营分析' && obj['OMPHBusinessAnalysis']) {
							list.push(s);
						} else if (s.name == '门禁管控' && obj['OMPHDevEntrance']) {
							list.push(s);
						} else if (s.name == '灯光管控' && obj['OMPHLampControl']) {
							list.push(s);
						} else if (s.name == '监控设备' && obj['OMPHSupervisory']) {
							list.push(s);
						} else if (s.name == '扫码核验' && obj['OMPHCodeVerification']) {
							list.push(s);
						} else if (s.name == '会员核验' && obj['OMPHMebVerification']) {
							list.push(s);
						} else if (s.name == '会员管理' && obj['OMPHMember']) {
							list.push(s);
						} else if (s.name == '意见反馈' && obj['OMPHFeedback']) {
							list.push(s);
						} else if (s.name == '活动核验' && obj['OMPHSignHx']) {
							list.push(s);
						} else if (s.name == '停车券' && obj['OMPHParkingDiscount']) {
							list.push(s);
						}
					});
					this.iconListb = list;
					this.userList = res.record;
				});
				this.$app.ajax({
					data: {
						param: {
							venueId: uni.getStorageSync('GYMID')
						}
					},
					api: this.$api.home()
				}).then((res) => {
					if (res.errorCode != '000000') return;
					this.headList = [{
							name: '今日营收',
							value: res.record.income
						},
						{
							name: '今日订单',
							value: res.record.orderCount
						},
						{
							name: '今日入场',
							value: res.record.peopleNum
						}
					];
				});
			},
			getDate() {
				let date = new Date();
				let nnn = date.getFullYear();
				let yyy = date.getMonth() + 1;
				let rrr = date.getDate();
				let xq = date.getDay();
				let data = '';
				switch (xq) {
					case 1:
						data = '周一';
						break;
					case 2:
						data = '周二';
						break;
					case 3:
						data = '周三';
						break;
					case 4:
						data = '周四';
						break;
					case 5:
						data = '周五';
						break;
					case 6:
						data = '周六';
						break;
					case 0:
						data = '周日';
						break;
				}
				let res = nnn + '年' + yyy + '月' + rrr + '日' + ' ' + data;
				this.date = res;
			}
		}
	};
</script>

<style lang="scss">
	.head {
		background: #f0f3fa;
		padding: 30rpx 30rpx 0;
		box-sizing: border-box;
	}

	.head-left {
		font-size: 26rpx;
		color: #666666;
		line-height: 1.5;
	}

	.head-left view:first-of-type {
		color: #222222;
		font-size: 32rpx;
		margin-bottom: 10rpx;
	}

	.head-right {
		color: #4167c1;
		font-size: 24rpx;
		line-height: 48rpx;
		border-radius: 26rpx;
		padding: 0 20rpx;
		border: 1px solid #4167c1;
	}

	.head-tj {
		background: linear-gradient(180deg, #4f76d4 0%, #4167c1 100%);
		border-radius: 16rpx 16rpx 0rpx 0rpx;
		padding: 30rpx 0;
		margin: 0 auto 30rpx;
	}

	.head-tjcnt {
		text-align: center;
		flex: 1;
		color: #ffffff;
		font-size: 24rpx;
		line-height: 1.5;
		color: #ffffff;
	}

	.head-tjcnt view:last-of-type {
		font-weight: bold;
		font-size: 28rpx;
		margin-top: 10rpx;
	}

	.head-tjcnt view text {
		font-size: 24rpx;
		font-weight: normal;
	}

	.icon {
		width: 20%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		color: #333333;
		font-size: 24rpx;
		margin-bottom: 30rpx;
	}

	.icon image {
		width: 88rpx;
		height: 88rpx;
		margin-bottom: 10rpx;
	}

	.content {
		padding: 0 30rpx;
	}

	.li {
		background-color: #fff;
		border-radius: 8rpx;
		padding: 30rpx;
		box-shadow: 0rpx 4rpx 24rpx 0rpx rgba(0, 0, 0, 0.08);
		box-sizing: border-box;
		margin-bottom: 20rpx;
	}

	.title {
		font-size: 28rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		line-height: 1.5;
		color: #222222;
	}

	.title text {
		font-size: 22rpx;
		color: #4167c1;
		line-height: 36rpx;
		border-radius: 4rpx;
		border: 1px solid #4167c1;
		padding: 0 10rpx;
		display: inline;
		margin-right: 20rpx;
	}

	.li.on .title text {
		border: 1px solid #dd5040;
		color: #dd5040;
	}

	.chuli {
		font-size: 26rpx;
		line-height: 1.5;
		color: #666666;
		margin-top: 30rpx;
	}

	.chuli .btn {
		width: 112rpx;
		display: inline;
		line-height: 50rpx;
		height: 52rpx;
		box-sizing: border-box;
		border-radius: 26rpx;
		border: 1px solid #4167c1;
		font-size: 24rpx;
		text-align: center;
		color: #4167c1;
	}

	.daiban {
		font-weight: 400;
		color: #999999;
		font-size: 24rpx;
		margin-top: 20rpx;
		line-height: 1.5;
	}

	.daiban view:first-of-type {
		color: #222222;
		font-size: 32rpx;
		font-weight: bolder;
	}
</style>